package com.zdy.viewpager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem;
import com.yalantis.taurus.PullToRefreshView;

public class FlyFragment extends Fragment {

	private FragmentActivity activity;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return inflater.inflate(R.layout.activity_pull_to_refresh, container,
				false);
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		activity = getActivity();
		int position = FragmentPagerItem.getPosition(getArguments());
		TextView title = (TextView) view.findViewById(R.id.item_title);
		title.setText(String.valueOf(position));

		Map<String, Integer> map;
		List<Map<String, Integer>> sampleList = new ArrayList<Map<String, Integer>>();

		int[] colors = { R.color.saffron, R.color.eggplant, R.color.sienna };

		int[] tripNames = { R.string.trip_to_india, R.string.trip_to_italy,
				R.string.trip_to_indonesia };

		for (int i = 0; i < tripNames.length; i++) {
			map = new HashMap<String, Integer>();
			map.put(SampleAdapter.KEY_NAME, tripNames[i]);
			map.put(SampleAdapter.KEY_COLOR, colors[i]);
			sampleList.add(map);
		}

		ListView listView = (ListView) view.findViewById(R.id.list_view);
		listView.setAdapter(new SampleAdapter(getActivity(),
				R.layout.list_item1, sampleList));

		mPullToRefreshView = (PullToRefreshView) view
				.findViewById(R.id.pull_to_refresh);
		mPullToRefreshView.setRefreshing(true);
		mPullToRefreshView.postDelayed(new Runnable() {
			@Override
			public void run() {
				mPullToRefreshView.setRefreshing(false);
			}
		}, REFRESH_DELAY);
		mPullToRefreshView
				.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
					@Override
					public void onRefresh() {
						mPullToRefreshView.postDelayed(new Runnable() {
							@Override
							public void run() {
								mPullToRefreshView.setRefreshing(false);
							}
						}, REFRESH_DELAY);
					}
				});

	}

	public static final int REFRESH_DELAY = 4000;

	private PullToRefreshView mPullToRefreshView;

	class SampleAdapter extends ArrayAdapter<Map<String, Integer>> {

		public static final String KEY_NAME = "name";
		public static final String KEY_COLOR = "color";

		private final LayoutInflater mInflater;
		private final List<Map<String, Integer>> mData;

		public SampleAdapter(Context context, int layoutResourceId,
				List<Map<String, Integer>> data) {
			super(context, layoutResourceId, data);
			mData = data;
			mInflater = LayoutInflater.from(context);
		}

		@Override
		public View getView(final int position, View convertView,
				@NonNull ViewGroup parent) {
			final ViewHolder viewHolder;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.list_item1, parent,
						false);
				viewHolder.textViewName = (TextView) convertView
						.findViewById(R.id.text_view_name);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}

			viewHolder.textViewName.setText(mData.get(position).get(KEY_NAME));
			convertView.setBackgroundResource(mData.get(position)
					.get(KEY_COLOR));

			return convertView;
		}

		class ViewHolder {
			TextView textViewName;
		}

	}
}
